package projectServlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dto.UserDTO;
import dao.DAOException;
import dao.EmployeeDAO;
import dao.EmployeeDAOFactory;

/**
 * b Servlet implementation class loginServlet
 */
@WebServlet("/login")
public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 * 
	 *      public loginServlet() { super(); // TODO Auto-generated constructor
	 *      stub }
	 * 
	 *      /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws DAOException, IOException {
		String userID;
		String password;
		String role;
		EmployeeDAO empDAO;
		String jspPage = "";
		role = request.getParameter("role");
		password = request.getParameter("password");
		userID = request.getParameter("userID");
		
		empDAO = EmployeeDAOFactory.loadInstance().getEmployeeDAO();
		
			if (userID.compareTo("") == 0 || password.compareTo("") == 0) {
			if (userID.compareTo("") == 0 && password.compareTo("") == 0) {
				request.setAttribute("emp", true);
			} else if (userID.compareTo("") == 0) {
				request.setAttribute("empUser", true);
			} else if(password.compareTo("")==0){
				try {
					request.setAttribute("empPassword", true);
				} catch (NumberFormatException nfe) {
					request.setAttribute("empPassword", true);
					request.setAttribute("notInt", true);
				}
			}
			jspPage = "login.jsp";
		}

		else {
			try {
				if (empDAO.login(Integer.parseInt(userID), password,role)) {

					if (role.compareTo("admin") == 0) {
						jspPage = "/AdminListPreset";
					} else {
						jspPage = "/viewdetail";
					}
				} else {

					request.setAttribute("flag", true);
					jspPage = "login.jsp";
				}

			} catch (NumberFormatException nfe) {
				request.setAttribute("notInt", true);
				jspPage = "login.jsp";

			}
		}

		RequestDispatcher rd = request.getRequestDispatcher(jspPage);
		callSession(request, userID,role);

		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();

		}
	}

	private void callSession(HttpServletRequest request, String userID, String role) {
		HttpSession session = request.getSession();

		session.setAttribute("id", userID);
		
		//for head.jsp 
		session.setAttribute("StaffRole",role);
	}

}
